<?php
ini_set('display_errors','On');
//ini_set('error_reporting',E_ALL);
define('PHPCMS_ROOT', str_replace("\\", '/', dirname(__FILE__).'/cms').'/');
define('IN_PHPCMS', TRUE);
include 'cms/include/config.inc.php';
include 'cms/include/global.func.php';
set_include_path('cms/include/');

function gCookie($name)
{
	if(isset($_COOKIE[COOKIE_PRE.$name]))
	{
		return $_COOKIE[COOKIE_PRE.$name];
	}
	return null;
}
function sCookie($name,$value)
{
	setcookie(COOKIE_PRE.$name,$value,time() + 86400);
}

function showmessage2($text,$url=null)
{
	$msg=$text;
	if($url==null)
	{
		$url_forward='';	
	}
	else
	{
		$url_forward=$url;
	}
	@require template('phpcms','showmessage');
	exit;
}

function my_json_encode($array)
{
	$str=array();
	foreach($array as $k=>$value)
	{
		if(is_array($value))
		{
			$str[]='"'.trim($k).'":'.my_json_encode($value);
		}
		else
		{
			$str[]='"'.trim($k).'":"'.str_replace('"','&quot;',trim($value)).'"';
		}
	}
	return '{'.join(',',$str).'}';
}

function my_json_decode($str)
{
	$str=str_replace('{','array(',$str);
	$str=str_replace(':','=>',$str);
	$str=str_replace('}',')',$str);
	eval('$a='.$str.';');
	return $a;
}

function descape($str) {
         $str = rawurldecode($str);
         preg_match_all("/%u.{4}|&#x.{4};|&#\d+;|.+/U",$str,$r);
         $ar = $r[0];
         foreach($ar as $k=>$v) {
                  if(substr($v,0,2) == "%u")
                           $ar[$k] = iconv("UCS-2","UTF-8",pack("H4",substr($v,-4)));
                  elseif(substr($v,0,3) == "&#x")
                           $ar[$k] = iconv("UCS-2","UTF-8",pack("H4",substr($v,3,-1)));
                  elseif(substr($v,0,2) == "&#") {
                           $ar[$k] = iconv("UCS-2","UTF-8",pack("n",substr($v,2,-1)));
                  }
         }
         return join("",$ar);
}

/*function descape($str)//对js的escape进行反编译
{
	$erg="/%u([\dabcdef]{4})/i";
	$_descape=create_function('$matches','echo $matches[1]."<br>";$onebit=intval(substr($matches[1],0,2),16);$twobit=intval(substr($matches[1],2,2),16);$a=chr($onebit).chr($twobit);echo $onebit." ".$twobit."<br>";var_dump($a);var_dump(iconv("GBK","UTF-8",$a));return iconv("GBK","UTF-8",$a);');
	preg_replace_callback($erg,$_descape,$str);
}

function _descape($match)
{
	var_dump($match);
	$a=chr(substr($match[0],1,2)).chr(substr($match[0],3,2));
	return iconv('GBK','UTF-8',$a);
}

/**/
?>